package com.ys.otms.controller;

import com.ys.otms.dto.HuoYuanDto;
import com.ys.otms.services.IDeductionsServices;
import com.ys.otms.services.IHuoYuanServices;
import com.ys.otms.utils.JsonUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Administrator on 2018/10/17.
 */
@Controller
@RequestMapping("/huoyuan")
public class HuoYuanController {
    private static Logger logger = LoggerFactory.getLogger(HuoYuanController.class);

    @Autowired
    private IHuoYuanServices iHuoYuanServices;

    @Autowired
    private IDeductionsServices iDeductionsServices;

    /**
     * 新增货源
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertCompanyHuoYuan")
    @ResponseBody
    public JSONObject insertCompanyHuoYuan(HttpServletRequest request) throws Exception{
        logger.info("HuoYuanController insertCompanyHuoYuan start");
        JSONObject jsonHuoYuan = new JSONObject();
        try {
            jsonHuoYuan = iHuoYuanServices.insertCompanyHuoYuan(request);
        } catch (Exception e) {
            logger.error("HuoYuanController insertCompanyHuoYuan Exception",e);
            jsonHuoYuan.put("SUCCESS",false);
            jsonHuoYuan.put("MSG","新增数据失败！");
        }
        return jsonHuoYuan;
    }

    /**
     * 查询货源
     * @param request
     * @return
     */
    @RequestMapping("/selectCompanyHuoYuan")
    @ResponseBody
    public JSONObject selectCompanyHuoYuan(HttpServletRequest request) throws Exception {
        logger.info("HuoYuanController selectCompanyHuoYuan start");
        JSONObject jsonHuoYuan = new JSONObject();
        try {
            jsonHuoYuan = iHuoYuanServices.selectCompanyHuoYuan(request);
        } catch (Exception e) {
            logger.error("HuoYuanController selectCompanyHuoYuan error", e);
            jsonHuoYuan.put("SUCCESS",false);
            jsonHuoYuan.put("MSG","查询数据失败！");
        }
        return jsonHuoYuan;
    }


    /**
     * 查询货源船舶
     * @param request
     * @return
     */
    @RequestMapping("/selectHuoYuanShip")
    @ResponseBody
    public JSONObject selectHuoYuanShip(HttpServletRequest request) throws Exception {
        logger.info("HuoYuanController selectHuoYuanShip start");
        JSONObject jsonHuoYuan = new JSONObject();
        try {
            jsonHuoYuan = iHuoYuanServices.selectHuoYuanShip(request);
        } catch (Exception e) {
            logger.error("HuoYuanController selectHuoYuanShip error", e);
            jsonHuoYuan.put("SUCCESS",false);
            jsonHuoYuan.put("MSG","查询数据失败！");
        }
        return jsonHuoYuan;
    }

    /**
     * 立即联系(查询电话号码)
     * @param request
     * @return
     */
    @RequestMapping("/selectHuoYuanShipByPhone")
    @ResponseBody
    public JSONObject selectHuoYuanShipByPhone(HttpServletRequest request) throws Exception {
        JSONObject obj = JsonUtil.getJSONObject(request);
        logger.info("HuoYuanController selectHuoYuanShipByPhone start");
        JSONObject jsonHuoYuan = new JSONObject();
        try {
            JSONObject jsonObject = iDeductionsServices.deductionsMethod(request,obj);
          if ("true".equals(jsonObject.getString("SUCCESS"))){
            jsonHuoYuan = iHuoYuanServices.selectHuoYuanShipByPhone(obj);
          }
        } catch (Exception e) {
            logger.error("HuoYuanController selectHuoYuanShipByPhone error", e);
            jsonHuoYuan.put("SUCCESS",false);
            jsonHuoYuan.put("MSG","查询数据失败！");
        }
        return jsonHuoYuan;
    }
}
